import Vue from 'vue'
import Vuex from 'vuex'
import Api from '@/api/index'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    isDev: null,
    userInfo: {},
    refs: null,
  },
  mutations: {
    setIsDev(state) {
      let appName = Vue.prototype.appName
      // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
      Api.environment.isDev({
        appName: appName
      }).then(res => {
        state.isDev = res[appName]
      })
      // #endif
      // #ifndef MP-WEIXIN || MP-TOUTIAO || MP-QQ
      state.isDev = 'release'
      // #endif
    },
    refreshUserInfo(state) {
      let token = uni.getStorageSync('token')
      if (token) {
        Api.user.getUserInfo().then(res => { // 获取用户信息
          if (res.data.id) {
            state.userInfo = res.data
          }
          // #ifndef H5
          if(res.data.mobile){ // 用户画像上报
            getApp().mtj.setUserInfo({
              tel: res.data.mobile,
              openId: res.data.openId || ''
            });
          }
          // #endif
        })
      }else{
        state.userInfo = {}
      }
    },
    resignRefs(state, refs){
      state.refs = refs
    },
  }
})

export default store
